



// 定义生命周期
const LIFECYCLE_HOOKS = [
  'beforeCreate',
  'created',
  'beforeMount',
  'mounted',
  'beforeUpdate',
  'updated',
  'beforeDestroy',
  'destroyed',
]

// 生命周期合并策略
function mergeHook(parent, child) {
  if (child) {
    if (parent) {
      return parent.concat(child)
    } else {
      return [child]
    }
  } else {
    return parent
  }
}
// 合并策略
const strats = {}

LIFECYCLE_HOOKS.forEach(k => {
  strats[k] = mergeHook
})

// mixin 核心方法
export default function mergeOptions(parent, child) {
  let options = {}
  for (let k in parent) {
    mergeFiled(k)
  }

  for (let k in child) {
    if (!parent.hasOwnProperty(k)) {
      mergeFiled(k)
    }
  }

  function mergeFiled(key) {
    if (strats[key]) {
      options[key] = strats[key](parent[key], child[key])
    } else {
      options[key] = child[key] ? child[key] : parent[key]
    }
  }
  return options
}